/*
 * @(#)BitmapManager.java	 2011-11-10
 *
 * Copyright 2004-2011 WXXR Network Technology Co. Ltd. 
 * All rights reserved.
 * 
 * WXXR PROPRIETARY/CONFIDENTIAL.
 */

package com.ykdl.member.kid.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

/**
 * @class desc A BitmapManager.图片管理类
 * 
 * @author liuzhongnan
 * @version $Revision: 1.8 $
 * @created time 2011-11-10 下午6:09:04
 */
public class BitmapManager {
	/**
	 * 从网上获得图片
	 * 
	 * @param url
	 * @return Bitmap bitmap
	 */
	public static Bitmap getBitmap(String url) {
		InputStream is = null;
		if (url != null) {
			is = getNetInputStream(url);
		}

		Bitmap bitmap = BitmapFactory.decodeStream(is);
		try {
			if (is != null) {
				is.close();
			}

		} catch (IOException e) {
			return null;
		}
		return bitmap;

	}

	private static InputStream getNetInputStream(String urlStr) {

		try {
			URL url = new URL(urlStr);
			URLConnection conn = url.openConnection();
			conn.connect();
			InputStream is = conn.getInputStream();
			return is;
		} catch (Exception e) {
		}
		return null;
	}

	public static Bitmap decreaseBitmap(String path) {
		BitmapFactory.Options opts = new BitmapFactory.Options();
		opts.inSampleSize = 6;
		Bitmap resizeBmp = BitmapFactory.decodeFile(path, opts);
		int w = resizeBmp.getWidth();
		int h = resizeBmp.getHeight();
		Matrix matrix = new Matrix();
		if (w > h) {
			matrix.postRotate(90, (float) 360 / 2, (float) 480 / 2);
			float scaleW = (float) 360 / h;
			float scaleH = (float) 480 / w;
			if (scaleW > 1 && scaleH > 1) {
				matrix.postScale(1, 1);
			} else {
				if (scaleW > scaleH) {
					matrix.postScale(scaleW, scaleW);
				} else {
					matrix.postScale(scaleH, scaleH);
				}
			}
		} else {
			float scaleW = (float) 360 / w;
			float scaleH = (float) 480 / h;
			if (scaleW > 1 && scaleH > 1) {
				matrix.postScale(1, 1);
			} else {
				if (scaleW > scaleH) {
					matrix.postScale(scaleW, scaleW);
				} else {
					matrix.postScale(scaleH, scaleH);
				}
			}
		}
		Bitmap newBitmap = Bitmap.createBitmap(resizeBmp, 0, 0, w, h, matrix,
				true);
		if (resizeBmp.isRecycled() == false) {
			resizeBmp.recycle();
			System.gc();
		}
		return newBitmap;
	}

	public static void saveBitmap(String filename,Bitmap bm ){  
        //String filename = "/mnt/sdcard/a.png";  
        if (null == bm || bm.getWidth() == 0 || bm.getHeight() == 0){  
            return;  
        }      
        File temp = new File(filename);  
        FileOutputStream fout = null;  
        try{  
            temp.createNewFile();  
            fout = new FileOutputStream(temp);  
        }catch (IOException e){  
            e.printStackTrace();  
        }  
//        bm.compress(Bitmap.CompressFormat.PNG, 50, fout);  
        
        try {  
            fout.flush();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        try {  
            fout.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        if (!bm.isRecycled()){  
            bm.recycle();  
        }  
    }  
}
